/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clientereligadora.paineis;

import clientereligadora.entidades.SelectionAdapter;
import clientereligadora.entidades.SelectionPainter;
import clientereligadora.formularios.FormPrincipal;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import org.jdesktop.swingx.JXMapKit;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.mapviewer.GeoPosition;
import org.jdesktop.swingx.mapviewer.Waypoint;
import org.jdesktop.swingx.mapviewer.WaypointPainter;
import org.jdesktop.swingx.mapviewer.WaypointRenderer;
import org.openstreetmap.gui.jmapviewer.Coordinate;
import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;

/**
 *
 * @author Leandro Aquino
 */
public class PainelMaps extends javax.swing.JPanel {
    JXMapKit jXMapKit = new JXMapKit();
    JLabel hoverLabel = new JLabel("Java");

    double Latitude, Longitude;
    String ponto;
    private boolean dragging;
	//private JXMapViewer viewer;

	private Point2D startPos = new Point2D.Double();
	private Point2D endPos = new Point2D.Double();
/**
     * Creates new form painelMaps
     */
    public PainelMaps() {
        initComponents();
      
       
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jMapViewer1 = new org.openstreetmap.gui.jmapviewer.JMapViewer();
        jLabel1 = new javax.swing.JLabel();

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setPreferredSize(new java.awt.Dimension(793, 463));

        jMapViewer1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jMapViewer1.setMapMarkerVisible(true);
        jMapViewer1.setName(""); // NOI18N
        jMapViewer1.setScrollWrapEnabled(true);
        jMapViewer1.setZoomButtonStyle(org.openstreetmap.gui.jmapviewer.JMapViewer.ZOOM_BUTTON_STYLE.VERTICAL);
        jMapViewer1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMapViewer1MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jMapViewer1MouseEntered(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMapViewer1MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jMapViewer1MouseReleased(evt);
            }
        });
        jMapViewer1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jMapViewer1MouseDragged(evt);
            }
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                jMapViewer1MouseMoved(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(220, 220, 220)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jMapViewer1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 793, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jMapViewer1, javax.swing.GroupLayout.DEFAULT_SIZE, 426, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jMapViewer1MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMapViewer1MouseEntered
   
        hoverLabel = new JLabel("Java");
        hoverLabel.setVisible(false);
         JXMapViewer map = new JXMapKit().getMainMap();
        //location of Java
        GeoPosition gp = new GeoPosition(-20.44782, -54.67891);
        //convert to world bitmap
        Point2D gp_pt = map.getTileFactory().geoToPixel(gp, map.getZoom());
        //convert to screen
        jXMapKit.setMiniMapVisible(true);
        Rectangle rect = map.getViewportBounds();
        Point converted_gp_pt = new Point((int) gp_pt.getX() - rect.x,
                (int) gp_pt.getY() - rect.y);
        
        //check if near the mouse
        if (converted_gp_pt.distance(evt.getPoint()) > 10) {
            hoverLabel.setLocation(converted_gp_pt);
            hoverLabel.setVisible(true);
        } else {
            hoverLabel.setVisible(false);
        }
        //System.out.println("converted_gp_pt: "+converted_gp_pt);
         System.out.println("map.getAddressLocation(): "+gp_pt);
        
      
    }//GEN-LAST:event_jMapViewer1MouseEntered

    private void jMapViewer1MouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMapViewer1MouseMoved
  
        JXMapViewer map = jXMapKit.getMainMap();
        //location of Java
        GeoPosition gp = new GeoPosition(-7.502778, 111.263056); 
        //convert to world bitmap
        Point2D gp_pt = map.getTileFactory().geoToPixel(gp, map.getZoom());
        //convert to screen
        Rectangle rect = map.getViewportBounds();
         Point converted_gp_pt = new Point((int)gp_pt.getX()-rect.x,
                                          (int)gp_pt.getY()-rect.y);
         
          if(converted_gp_pt.distance(evt.getPoint()) > 10) {
                 hoverLabel.setLocation(converted_gp_pt);
            hoverLabel.setVisible(true);
            }else{
              hoverLabel.setVisible(false);
          }
    }//GEN-LAST:event_jMapViewer1MouseMoved
public void addWaypoint() {
    //create a Set of waypoints
    
    Set<Waypoint> waypoints = new HashSet<>();
    waypoints.add(new Waypoint(41.881944,-87.627778));
    waypoints.add(new Waypoint(40.716667,-74));
    
    //crate a WaypointPainter to draw the points
    WaypointPainter painter = new WaypointPainter();
    painter.setWaypoints(waypoints);
   
    
    painter.setRenderer(new WaypointRenderer() {
            @Override
    public boolean paintWaypoint(Graphics2D g, JXMapViewer map, Waypoint wp) {
        g.setColor(Color.RED);
        g.drawLine(-5,-5,+5,+5);
        g.drawLine(-5,+5,+5,-5);
        return true;
    }
});
    System.out.println(painter);
     jXMapKit.getMainMap().setOverlayPainter(painter);
     
     SelectionAdapter sa = new SelectionAdapter(jMapViewer1); 
		 SelectionPainter sp = new  SelectionPainter(sa); 
		jMapViewer1.addMouseListener(sa); 
		jMapViewer1.addMouseMotionListener(sa); 
		//jMapViewer1.setOverlayPainter(sp);
}
    private void jMapViewer1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMapViewer1MouseClicked
    
    
        List<MapMarker> teste = jMapViewer1.getMapMarkerList();
        for (int i = 0; i < teste.size(); i++) {
          Latitude=  teste.get(i).getLat();
          Longitude =  teste.get(i).getLon();
          
              System.out.println(Latitude+","+Longitude+"\n");
        }
        
         ponto = ""+teste ;
          ponto = ponto.replace("MapMarker at ", "");
                  
        System.out.println("lista: " +ponto);
      
        JMenuItem item3 = new JMenuItem(""+Latitude+""+Longitude);
        JPopupMenu popup = new JPopupMenu();
        popup.add(item3);
        // popup.show();
        //  jMapViewer1.add(popup);]

        popup.show(jMapViewer1, 50, 50);
        System.out.println("Leandro"+PainelMaps.jMapViewer1.getMapPosition(Latitude, Longitude).getLocation());
       // addWaypoint() ;

//        DefaultMapController mapController = new DefaultMapController(PainelMaps.jMapViewer1);
//        int btn1 = MouseEvent.BUTTON1;
//        int btn2 = MouseEvent.BUTTON2;
//        Coordinate p = jMapViewer1.getPosition();
//    //btn2 = mapController.setMovementMouseButton(MouseEvent.BUTTON2);
//        if( mapController.getMovementMouseButton() == btn2){
//            System.out.println("P: "+p);
//             JMenuItem item3 = new JMenuItem("Opção 3");
//             JPopupMenu popup = new JPopupMenu();
//             popup.add(item3);
//              popup.show();
//        } else{
//             JMenuItem item2 = new JMenuItem("Opção 2");
//             JPopupMenu popup = new JPopupMenu();
//             popup.add(item2);
//              popup.show();
//        }

        //mostra na tela
   
    }//GEN-LAST:event_jMapViewer1MouseClicked

    private void jMapViewer1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMapViewer1MousePressed
//        JPopupMenu menu = new JPopupMenu();
//        JLabel lbl1 = new JLabel();
//         Coordinate p = jMapViewer1.getPosition();
//         
//        if ((evt.getModifiers() & MouseEvent.BUTTON2) != 0) {
//
////            JMenuItem item = new JMenuItem("Clique-me");
////            menu.add(p.toString());
////            System.out.println("b1" +p);
////             lbl1.setToolTipText(p.toString());
////            lbl1.setText(p.toString());
////           
////            menu.add(lbl1);
////             menu.show();
////              jMapViewer1.add(menu);
//              
//            JMenuItem item3 = new JMenuItem(p.toString());
//            JPopupMenu popup = new JPopupMenu();
//            popup.add(item3);
//            //popup.show();
//            //jMapViewer1.add(popup);
//               popup.show(jMapViewer1, 10, 10);
//           
//        } else {
//            JMenuItem item2 = new JMenuItem(p.toString());
//            JPopupMenu popup = new JPopupMenu();
//            popup.add(item2);
//              //jMapViewer1.add(popup);
//                 popup.show(jMapViewer1, 10, 10);
//          // popup.show();
////             JMenuItem item = new JMenuItem("Clique-me");
////            menu.add(p.toString());
////            System.out.println("b1" +p);
////            menu.show();
////              lbl1.setToolTipText(p.toString());
////              lbl1.setText(p.toString());
////                  menu.add(lbl1);
////         menu.show();
////         jMapViewer1.add(lbl1);
////           jMapViewer1.add(menu);
//        }
//       //  jMapViewer1.add(lbl1);
        if (evt.getButton() != MouseEvent.BUTTON3)
			return;

		startPos.setLocation(evt.getX(), evt.getY());
		endPos.setLocation(evt.getX(), evt.getY());

		dragging = true;
       
        

    }//GEN-LAST:event_jMapViewer1MousePressed

    private void jMapViewer1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMapViewer1MouseDragged
        if (!dragging) {
            return;
        }

        endPos.setLocation(evt.getX(), evt.getY());

        jMapViewer1.repaint();
    }//GEN-LAST:event_jMapViewer1MouseDragged

    private void jMapViewer1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMapViewer1MouseReleased
int a = evt.getButton();
if(a ==MouseEvent.BUTTON1){
    System.out.println("botao1");
}
if(a ==MouseEvent.BUTTON2){
    System.out.println("botao1");
}
if(a ==MouseEvent.BUTTON1){
    System.out.println("botao1");
}
        if (!dragging) {
            return;
        }

        if (evt.getButton() != MouseEvent.BUTTON3) {
            return;
        }

        jMapViewer1.repaint();

        dragging = false;
    }//GEN-LAST:event_jMapViewer1MouseReleased
public void exibirMapa(){
    jMapViewer1.setDisplayPositionByLatLon(-20.475, -54.629, 12);
   
}
public void exibirMapa1(double lat, double lon){
    jMapViewer1.setDisplayPositionByLatLon(lat, lon, 12);
   
}  
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    public static org.openstreetmap.gui.jmapviewer.JMapViewer jMapViewer1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
